//
//  FindViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FindViewController.h"
#import "Header.h"
#import "RecordViewController.h"
#import "Find.h"
#import "FindCollectionCell.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
#import "FJC_SearchViewController.h"
@interface FindViewController ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong)BaseCollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"发现";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
#pragma mark 搜索栏按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    
    [self createView];
    [self createData];

}

- (void)barAction:(UIButton *)button{
#pragma mark 跳转搜索栏界面
    FJC_SearchViewController *SearchVC = [[FJC_SearchViewController alloc] init];
    [self.navigationController pushViewController:SearchVC animated:YES];
}

- (void)createData {
    
    
    // 请求分类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.miaopai.com/m/discover.json?timestamp=1463474171067&unique_id=52f6af6d-8f1e-3cae-ad01-b5cd4b51e6c0&os=android&per=20&token=&page=1&vend=miaopai&udid=52f6af6d-8f1e-3cae-ad01-b5cd4b51e6c0&uuid=52f6af6d-8f1e-3cae-ad01-b5cd4b51e6c0&channel=mobile360&deviceId=52f6af6d-8f1e-3cae-ad01-b5cd4b51e6c0&version=6.3.5" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject;
        NSArray *result = dic[@"result"];
        self.dataArr = [NSMutableArray array];
        for (NSDictionary *dic in result) {
            Find *find = [[Find alloc] init];
            [find setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:find];
        }
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"联网失败");
        //        NSDictionary *dic = [self getDataWithDataName:@"hotMovieTable"];
        //        if (dic == nil) {
        //            NSLog(@"请连网啊亲");
        //        } else {
        //            NSDictionary *data = dic[@"data"];
        //            NSArray *hot = data[@"hot"];
        //            self.movieArr = [AGLMovie baseModalByArr:hot];
        //            [self.tableView reloadData];
        //        }
        //
        
    }];
}

- (void)createView {
    
    // 背景view
    BaseView *backView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    [self.view addSubview:backView];
    backView.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1];
    BaseLabel *titleLabel = [[BaseLabel alloc] initWithFrame:CGRectMake(15, 5, 100, 20)];
    [backView addSubview:titleLabel];
    titleLabel.text = @"精选栏目";
    titleLabel.textColor = [UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1];
    titleLabel.font = [UIFont systemFontOfSize:13];
    
    // 最近看过的图片
    BaseImageView *imageView = [[BaseImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 5, 20, 20)];
    [backView addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"liulan.png"];
    
    // 最近看过的button
    BaseButton *recordButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    recordButton.frame = CGRectMake(SCREEN_WIDTH - 80, 5, 90, 20);
    [backView addSubview:recordButton];
    [recordButton setTitle:@"最近看过" forState:UIControlStateNormal];
    [recordButton setTitleColor:[UIColor colorWithRed:168 / 255.0 green:168 / 255.0 blue:168 / 255.0 alpha:1] forState:UIControlStateNormal];
    recordButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    recordButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [recordButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 3, 150);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 30 ) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[FindCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([FindCollectionCell class])];
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (BaseCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FindCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FindCollectionCell class]) forIndexPath:indexPath];
    Find *find = self.dataArr[indexPath.row];
    cell.nameLabel.text = find.name;
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:find.icon] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
     Find *find = self.dataArr[indexPath.row];
    if ([find.type isEqualToString:@"rewardList"]) {
        
    }else if([find.type isEqualToString:@"user"])  {
   
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    NSString *str = [find.url substringFromIndex:39];
    detailVC.str = str;
    detailVC.findModel = find;
    NSLog(@"%@",str);
    }
}


- (void)click:(BaseButton *)button {
    RecordViewController *recordVC = [[RecordViewController alloc] init];
    [self.navigationController pushViewController:recordVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
